package tmall.framework.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import tmall.framework.eobj.EObj;

import javax.annotation.Resource;
import java.util.List;

@Data
public class AbstractServiceMapper<T extends EObj, E extends BaseMapper<T>> implements ServiceMapper<T> {

    public static final String LOGIC_DELETE = "1";
    public static final String LOGIC_ENABLE = "0";

    @Resource
    protected transient E mapper;

    @Override
    public T queryEObjById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public IPage<T> queryEObjByCondition(T input, IPage<T> page) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.setEntity(input);
        return mapper.selectPage(page, wrapper);
    }

    @Override
    public boolean deleteLogicEObjById(T input) {
   /*    QueryWrapper<T> wrapper=new QueryWrapper<>();
        Class<?>  clz=  wrapper.getEntity().getClass();
        Field[] Fields = clz.getDeclaredFields();
        String pkName = "";
        for(Field Field : Fields){
            Id pkId =  Field.getAnnotation(Id.class);
            if(pkId != null){
              pkName =  Field.getName();
                break;
            }
        }
        if(StringUtils.isEmpty(pkName)){
            throw new TException(ErrorEnum.PK_IS_EXIST,new Object[]{clz.getName()});
        }
        wrapper.eq(pkName,id);

        EObj input = new T();
        input.setStatus(LOGIC_DELETE);*/
        mapper.updateById(input);
        return true;
    }

    @Override
    public boolean deletePhysicalEObjById(Long id) {
        mapper.deleteById(id);
        return true;
    }

    @Override
    public T addEObj(T input) {
        mapper.insert(input);
        return input;
    }

    @Override
    public boolean batchAddEObj(List<T> input) {
        //  this.getBaseMapper().b
        return true;
    }


    @Override
    public T updateEObjById(T input) {
        mapper.updateById(input);
        return input;
    }

    @Override
    public T updateEObjByCondition(T paramEObj, T updateEObj) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();

        mapper.update(updateEObj, wrapper);
        return updateEObj;
    }
}
